package org.fuxia.im.core.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.fuxia.im.core.entries.ECPacket;
import org.fuxia.im.core.entries.ECResponse;
import org.fuxia.im.core.utils.ObjectUtil;
import org.fuxia.im.db.dao.UserGroupDaoImpl;
import org.fuxia.im.entities.UserGroup;

/**
 * 用户分组管理
 * @author Administrator
 *
 */
public class UserGroupService extends AbstractService{
	
	@Resource
	private UserGroupDaoImpl userGroupDao ;
	
	public void service(ECPacket packet) {
		if("add".equals(packet.getParameter("option"))) {
			addGroup(packet) ;
		}
	}

	@SuppressWarnings("rawtypes")
	public void addGroup(ECPacket packet) {
		ECResponse resp = new ECResponse() ;
		UserGroup userGroup = new UserGroup() ;
		String groupName = packet.getParameter("groupName") ;
		String userCode = packet.getParameter("userCode") ;
		userGroup.setGroupName(groupName) ;
		userGroup.setUserCode(userCode) ;
		String hql = "select ug.id from UserGroup ug where ug.groupName=?" ;
		try{
			userGroupDao.save(userGroup) ;
			List userGroups = userGroupDao.find(hql, new String[]{groupName}) ;
			resp.put("groupId", ObjectUtil.getJsonStr(userGroups.get(0))) ;
			resp.put("groupName", groupName) ;
			resp.setStatusCode(200) ;
		}catch(Exception e) {
			e.printStackTrace() ;
			resp.setStatusCode(400) ;
			resp.setResponseText("添加分组失败！") ;
		}
		resp.setResponseType("addUserGroup") ;
		try{
			writeReponse(packet, resp) ;
		}catch(Exception e) {
			e.printStackTrace() ;
		}
	}
	

}
